<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Nav;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class NavsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Nav::orderBy('nav_order','asc')->get();
        return view('admin/navs/index',compact('data'));
    }
    public function changeOrder()
    {
        $input = Input::all();
        $navs = Nav::find($input['nav_id']);
        $navs->nav_order = $input['nav_order'];
        $res = $navs->update();
        if($res){
            $data = [
                'status' => 0,
                'msg' => '自定义导航排序更新成功',
            ];
        }else{
            $data = [
                'status' => 1,
                'msg' => '自定义导航排序更新失败,请重新提交',
            ];
        }
        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin/navs/add');
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = Input::except('_token');
        $rules = [
            'nav_name'=>'required',

        ];
        $message = [
            'nav_name.required'=>'导航名称不能为空',
        ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $rs = Nav::create($input);
            if($rs){
                return redirect('admin/navs');
            }else{
                return back()->with('error','数据填写失败,请稍后重试');
            }
        }else{
            return back()->withErrors($validator);
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($nav_id)
    {
        $field = Nav::find($nav_id);
        return view('admin/navs/edit',compact('field'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($nav_id)
    {
        $input = Input::except('_token','_method');
        $rs = Nav::where('nav_id',$nav_id)->update($input);
        if($rs){
            return redirect('admin/navs');
        }else{
            return back()->with('error','自定义导航更新失败,请稍后重试');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($nav_id)
    {
        $rs = Nav::where('nav_id',$nav_id)->delete();
        if($rs){
            $data = [
                'status'=>0,
                'msg'=>'导航删除成功!'
            ];
        }else{
            $data= [
                'status'=>1,
                'msg'=>'导航删除失败,请稍后重试!'
            ];
        }
        return $data;
    }
}
